//
//  mainViewTableCall.m
//  garbageclient
//
//  Created by Wang Wu on 11-8-19.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "mainViewTableCall.h"
#import "commondef.h"

@implementation mainViewTableCall

@synthesize delegate;

- (void)dealloc
{
    [super dealloc];
}

- (id)initWithDictionary:(NSDictionary *)dic reuseIdentifier:(NSString *)identifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (self != nil) 
    {
        CGRect screen = [[UIScreen mainScreen] applicationFrame];
        
        self.frame = CGRectMake(0.0, 0.0, screen.size.width, TVC_HEIGHT);
        self.textLabel.text = [dic objectForKey:@"Ti"];
        
        NSString *strurl = [dic objectForKey:@"Si"];
        
        self.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; 
        UIActivityIndicatorView  *avt = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10, 10, 24, 24)];
        [avt setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
		[self.imageView addSubview:avt];
        [avt startAnimating];
		[avt release];
        
        [NSThread detachNewThreadSelector:@selector(loadImages:) toTarget:self withObject:strurl];
        
        myID = [dic objectForKey:@"ID"];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    NSString *strurl = [NSString stringWithFormat:@"http://appsearch.sagetouch.com/app123/iphoneappinfo/%d.html",[myID longValue]];
    [delegate showDetails:strurl];
}

#pragma mark-thread
-(void)loadImages:(NSString *)strurl{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
	NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strurl]];
    UIImage *image = [UIImage imageWithData:imageData];
    
    [self performSelectorOnMainThread:@selector(fillImage:) withObject:image  waitUntilDone:NO];
    
	[pool release];
}

-(void)fillImage:(UIImage *)image{
	
	[self.imageView setImage:image];
    
	for(id obj in self.imageView.subviews)
	{
		if([obj isKindOfClass:[UIActivityIndicatorView class]])
			[obj removeFromSuperview];
	}
}
@end
